<template>
  <div class="login-vue" style="background-size: cover;" :style="{backgroundImage:bgImg}">
    <div style="bottom:20px;text-align: center;width: 100%;color: #9b9b9b;font-size: 14px;position: absolute;" >{{copyright}}</div>
    <div class="login-container">
      <div class="login-content">
        <div class="login-title">{{ title }}</div>

        <div class="login-input">
          <input type="text" name="username" v-model="username" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: username != '' && username != null}">{{t('Username')}}</span>
        </div>
        <div class="login-password login-input">
          <input type="password" name="password" v-model="password" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: password != '' && password != null}">{{t('Password')}}</span>
        </div>
        <div v-if="needChangePassword" class="login-password login-input">
          <input type="password" name="new_password" v-model="new_password" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: new_password != '' && new_password != null}">{{t('NewPassword')}}</span>
          <div style="text-align: left;position: absolute;font-size: 10px;color: #7e7e7e;">
            <i class="h-icon-info" style="margin-right: 3px"></i>8位以上,并包含大小写字母、数字及符号
          </div>
        </div>
        <div v-if="needChangePassword" class="login-password login-input">
          <input type="password" name="new_password_re" v-model="new_password_re" autocomplete="off"/>
          <span class="placeholder" :class="{fixed: new_password_re != '' && new_password_re != null}">{{t('RepeatNewPassword')}}</span>
        </div>
        <div class="login-input">
          <input type="text" name="verify" v-model="verify" @keyup.enter="submit" autocomplete="off"
                 style="width: 210px"/>
          <span class="placeholder" :class="{fixed: verify != '' && verify != null}">{{ t('Captcha') }}</span>
          <img style="width: 100px;height:40px;cursor: pointer;vertical-align: bottom;" :src="verifyUrl" @click="updateVerify"/>
        </div>
        <div class="buttonDiv">
          <Button :loading="loading" block color="primary" size="l" @click="submit">{{t('Login')}}</Button>
        </div>
        <div>
          <LinkButtons>
            <Button @click="changeLang('zh')">中文</Button>
            <Button @click="changeLang('en')">English</Button>
          </LinkButtons>
        </div>
      </div>
    </div>
  </div>

</template>
<script setup>
import {ajax} from 'framework'
import {message} from 'HeyUI'
import {ref,getCurrentInstance} from 'vue'
import {encrypt,md5} from 'crypto'
import {useStore,loadUserInfo} from 'store'
import {setLanguage, useI18n} from "./js/i18n.js";

let store = useStore()

let title = document.title//'EasyAdmin'
let verifyUrl = ref('captcha?t=' + new Date().getTime())
let verify = ref('');//,loading = ref(false), username = ref(''),password = ref('')
const loading = ref(false),
    username = ref(),
    password = ref(),
    new_password=ref(),
    new_password_re=ref();
const needChangePassword=ref(false)
//let loadUserInfo = () => loadUserInfo()
let copyright=ref(window._configs["PlatformConfig.CopyRight"])
let bgImg=_configs["PlatformConfig.LoginBgImg"]?"url("+_configs["PlatformConfig.LoginBgImg"]+")":null

function updateVerify() {
  verifyUrl.value = 'captcha?t=' + new Date().getTime();
  verify.value = ''
}
const reg=/(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[!@#$%^&*.=\-\+()]).+/
function submit() {
  if (!username.value) {
    message(t("用户名不能为空"));
    return;
  }
  if (!password.value) {
    message(t("密码不能为空"));
    return;
  }
  if (!verify.value) {
    message(t("验证码不能为空"));
    return;
  }
  if (isNaN(verify.value)) {
    message(t("验证码错误"));
    return;
  }
  let pwd=encrypt(password.value,verify.value)
  const params={username: username.value, password: pwd, verify: verify.value}
  if(needChangePassword.value){
    if (!new_password.value) {
      message(t("新密码不能为空"));
      return;
    }
    if(new_password.value.length<8){
      message("新密码至少需要8位，且包含大小写字母、数字和符号");
      return;
    }
    if(!reg.test(new_password.value)){
      message("新密码需要包含大小写字母、数字和符号");
      return;
    }
    if (new_password.value!=new_password_re.value) {
      message("新密码与重复的新密码不一致");
      return;
    }
    params.new_password=new_password.value
  }
  loading.value = true;
  ajax.formPost("system/login",params).then((r) => {
    console.log("login",r)
    if (r.success){
      sessionStorage.uk=r.data.uk
      sessionStorage.un=r.data.un
      sessionStorage._token=r.data.token
      loadUserInfo().then(res => {

        loading.vaue = false;
      }).catch(e => {
        updateVerify()
        loading.value = false;
      })
    }
  }).catch((e) => {
    console.log(e)
    if(e.code==5000){
      needChangePassword.value=true
    }
   // message(e.message)
    //更新验证码
    updateVerify()
    loading.value = false;
  })
}
const {t}=useI18n({
  zh:{
    Username:"用户名",
    Password:"密码",
    Captcha:"验证码",
    NewPassword:"新密码",
    RepeatNewPassword:"重复新密码",
    Login:"登录"
  },
  en:{
    用户名不能为空: "the username cannot be empty",
    密码不能为空:"password cannot be empty",
    验证码不能为空:"captcha cannot be empty",
    验证码错误:'captcha error',
    新密码不能为空:"new password cannot be empty"
  }
})
const instance=getCurrentInstance()
//语言相关
function changeLang(lang){
  setLanguage(lang)
 // loadI18nMessages()
}
</script>
<style scoped>
.login-vue {
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: #f7f8fa;
}

.login-vue .login-container {
  width: 380px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login-vue .login-container .login-content {
  letter-spacing: 2px;
  background: #FFF;
  padding: 70px 30px 20px;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.06);
  border-radius: 3px;
  box-sizing: border-box;
}

.login-vue .login-container .login-content > div {
  margin: 30px 0;
}

.login-vue .login-container .login-content > div.login-input {
  position: relative;
}

.login-vue .login-container .login-content > div.login-input .placeholder {
  position: absolute;
  color: #7e7e7e;
  top: 6px;
  font-size: 16px;
  transition: all .2s;
  left: 0;
  pointer-events: none;
}

.login-vue .login-container .login-content > div.login-input input {
  font-size: 16px;
  padding: 8px 0;
  height: 40px;
  width: 100%;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #d3d3d3;
  box-shadow: inset 0 0 0 1000px #fff;
  outline: none;
  box-sizing: border-box;
  transition: .3s;
  font-weight: 200;
}

.login-vue .login-container .login-content > div.login-input input:focus {
  border-bottom-color: #3788ee;
  box-shadow: inset 0 0 0 1000px #fff;
}

.login-vue .login-container .login-content > div.login-input input:focus + .placeholder,
.login-vue .login-container .login-content > div.login-input .placeholder.fixed {
  font-size: 13px;
  top: -16px;
}

.login-vue .login-container .login-content > div.login-input input:-webkit-autofill + .placeholder {
  font-size: 13px;
  top: -16px;
}

.login-vue .login-container .login-content > div.login-title {
  font-size: 30px;
  color: #3a3a3a;
  line-height: 1;
  margin: -16px 0px 40px;
  font-weight: 200;
}

.login-vue .login-container .login-content > .buttonDiv {
  margin-top: 45px;
}

.login-vue .login-container .login-content > .buttonDiv .h-btn {
  padding: 12px 0;
  font-size: 18px;
  opacity: .8;
  border-radius: 3px;
  background: #3788ee;
  border-color: #3788ee;
}

.login-vue .login-container .login-content > .buttonDiv .h-btn:hover {
  opacity: 1;
  background: #3788ee;
  border-color: #3788ee;
}

.login-vue .login-container .copyright {
  letter-spacing: 1px;
  margin-top: 30px;
  color: #7e7e7e;
}

.login-vue .login-container .copyright a {
  color: #7e7e7e;
}

</style>
